package daosimpl;

import java.util.ArrayList;
import java.util.List;

import model.Curso;
import model.Profesor;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;




import util.HibernateUtil;
import daos.ProfesorDAO;

public class ProfesorDAOImplH implements ProfesorDAO {
	private Transaction tx;

	@Override
	public boolean ValidarProfesor(Profesor p) throws HibernateException {
		boolean result = false;
		try {
			
			tx = HibernateUtil.session.beginTransaction();
						
			Query query = HibernateUtil.session
					.createQuery("FROM Profesor where docenteId = '" + p.getDocenteId()
							+  "'");
			String pass = ((Profesor) query.list().get(0)).getPassword();
			System.out.println("dao: " + pass);
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}
			
			System.out.println("dao: " + pass);
			if(pass.equals(p.getPassword())){
				result = true;
				System.out.println("true");
			}
			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result;        
	}
	
	@Override
	public Profesor ValidarProfesor2(Profesor p) throws HibernateException {
		Profesor result = null;
		try {
			
			tx = HibernateUtil.session.beginTransaction();
						
			Query query = HibernateUtil.session
					.createQuery("FROM Profesor where docenteId = '" + p.getDocenteId()
							+  "'");
			result = (Profesor) query.list().get(0);
			
			if(!p.getPassword().equals(result.getPassword())){
				return null;
			}
			
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}
			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result;        
	}
	
	public Profesor FindById(String id) throws HibernateException{
		Profesor result = new Profesor();
		try {
			
			tx = HibernateUtil.session.beginTransaction();
						
			Query query = HibernateUtil.session
					.createQuery("FROM Profesor where docenteId = '"+
			id +"'");			
			System.out.println("PROFESORES "+query.list().size());
			System.out.println("PROFESORES "+query.list().size());
			System.out.println("PROFESORES "+query.list().size());			
			System.out.println("PROFESORES "+query.list().size());
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
				result=null;
			}
			result = (Profesor) query.list().get(0);
			System.out.println("LISTA PROFESORES "+query.list().size());
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result; 
		
	}
	
	public List<Profesor> ListarProfesores ()throws HibernateException{
		List<Profesor> result = new ArrayList<Profesor>();
		try {
			
			tx = HibernateUtil.session.beginTransaction();
						
			Query query = HibernateUtil.session
					.createQuery("FROM Profesor");
			result = query.list();
			//System.out.println("PROFESORES"+query.list());
			//System.out.println(query.list().size());
			if (query.list().isEmpty()) {
				System.out.println("esta vacio");
			}
			tx.commit();
			if (!tx.wasCommitted()){
			    tx.commit();
			}			
			return result;
			
		} catch (Exception he) {
			System.out.println("mall " + he.getMessage() + he.getCause());
			tx.rollback(); 
		} finally { 
            //session.close(); 
        }  
		return result; 
	}

}
